// pages/submitOrders/submitOrders.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickupTime:'2019-01-02',//提货时间
    pickupAddress:'苏州园区星湖街纳米园A1南座5楼',//提货点
    community:'赵丽颖',//社区长
    numberPerson:'18920203344',//负责人电话
    thename: '',//提货人
    theNumber: '',//手机号
    theAddress: '',//收货地址
    paystyle:true,//支付方式
    chooseStyle:'微信支付',//所选支付方式
    balance:'200',//余额
    preferential: '微客币抵扣30元',//使用优惠
    totalAmount: '320.00',//商品总额
    preferentialGoods: '0.00',//商品优惠
    numberGoods:'2',//商品件数
    subtotal:'320.00',//小计
    total:'320',//共计
    haveDiscount:false,//优惠状态(true为已优惠，false为不足待充值)
    suborder:false,//判断提交订单状态
    paymentMethodBox: "none",//选择支付方式弹框
    
    //订单
    orders:[
      {
        orderimg:'../../images/goods1.png',
        orderTitle:'商品大标题',
        goodsprice:'80',
        goodsNum:'2',
      },
      {
        orderimg: '../../images/goods1.png',
        orderTitle: '商品大标题',
        goodsprice: '80',
        goodsNum: '2',
      }
    ],

    //弹框
    items: [
      { name: '微信', value: '微信支付', image: '../../images/weixin.png' },
      { name: '余额', value: '余额', image:'../../images/balance.png', checked: 'true' },
    ],
  },

  //选择支付项
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
  },

  //选择支付方式
  paymentStyle:function(){
    var that = this;
    that.setData({
      showView: that.showView = true
    })
  },
  //关闭弹框
  haschecked:function(){
    var that = this;
    that.setData({
      showView: that.showView = false
    })
  },

  //去充值
  toPrepaid:function(){
    wx.navigateTo({
      url: '../positioning/positioning'
    })
  },

  // 提货人监听输入
  watchThename: function (event) {
    var that =this;
    var thename = event.detail.value;
    var theNumber = that.data.theNumber;
    var theNumberlength = theNumber.length
    var theAddress = that.data.theAddress; 
    if (thename == "" || theNumber == "" || theNumberlength != 11 || theAddress == ""){
      that.setData({
        suborder:true,
        thename: thename,
      });
    } else{
      that.setData({
        suborder: false,
        thename: thename,
      });
    }
  },
  //手机号监听
  watchTheNumber: function (event) {
    var that = this;
    var thename = that.data.thename;
    var theNumber = event.detail.value;
    var theNumberlength = theNumber.length
    var theAddress = that.data.theAddress;
    if (thename == "" || theNumber == "" || theNumberlength != 11 || theAddress == "") {
      console.log("此时手机号为空或错误")
      that.setData({
        suborder: true,
        theNumber: theNumber,
      });
    } 
    else {
      that.setData({
        suborder: false,
        theNumber: theNumber,
      });
    }
  },
  //监听收货地址
  watchTheAddress: function (event) {
    var that = this;
    // console.log(event);
    console.log(event.detail.value);
    var thename = that.data.thename;
    var theNumber = that.data.theNumber;
    var theNumberlength = theNumber.length;
    var theAddress = event.detail.value;
    if (thename == "" || theNumber == "" || theNumberlength != 11 || theAddress == "") {
      console.log("此时收货为空")
      that.setData({
        suborder: true,
        theAddress: theAddress,
      });
    } else {
      that.setData({
        suborder: false,
        theAddress: theAddress,
      });
    }
  },

  //提交订单
  submitOrders:function(){
    console.log("submitOrders")
    var that = this;
    var thename = that.data.thename;
    var theNumber = that.data.theNumber;
    var theNumberlength = theNumber.length;
    var theAddress = that.data.theAddress;
    var thesuborder = that.data.suborder;
    // console.log(that.data.suborder)
    // console.log(1,thename,2, theNumber,3, theNumberlength, 4,theAddress, 5,thesuborder)
    if (thesuborder) {//此时suborder为true,不能提交
      console.log("zhelishitrue")
      if (thename == "" ){
        wx.showToast({
          title: '请填写收货人姓名',
          duration: 2000,
          icon: "none"
        })
      } else if (theNumber == "" || theNumberlength != 11 ){
        wx.showToast({
          title: '手机号为空或错误',
          duration: 2000,
          icon: "none"
        })
      } else if (theAddress == ""){
        wx.showToast({
          title: '请填写正确收货地址',
          duration: 2000,
          icon: "none"
        })
      }
    } else {//此时suborder为false,能提交
      console.log("zhelifou")
      wx.navigateTo({ //去成功页面，此处还缺少判断支付成功
        url: '/pages/paymentSuccess/paymentSuccess'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // console.log(that);
    var thename = that.data.thename;
    var theNumber = that.data.theNumber;
    var theNumberlength = theNumber.length;
    var theAddress = that.data.theAddress;
    if (thename == "" || theNumber == "" || theNumberlength != 11 || theAddress == "") {
      // console.log(123)
      that.setData({
        suborder: true,
      });
    } else {
      that.setData({
        suborder: false,
      });
    }
    console.log(thename, theNumber,theAddress);

   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // console.log(this)
    var that = this;
    that.setData({
      showView: that.showView = false
    })
     
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //支付方式弹框
    // that.setData({
    //   paymentBox: "none",
    // });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})